﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity.Model;
using BLL;
using SystemFrameWork;

namespace DutyWeb.UserControls
{
    public partial class LeftMenuTree : BaseUserControl
    {
        /// <summary>
        /// 
        /// </summary>
        private Sys_LeftMenuTreeManagement sys_LeftMenuManagement = new Sys_LeftMenuTreeManagement();

        private List<Guid?> selectedFunctionModule;

        public List<Guid?> SelectedFunctionModule
        {
            get
            {
                foreach (TreeNode tr in this.tvLeftMenu.Nodes)
                {
                    this.goThroughTheTree(tr);
                }
                return selectedFunctionModule;
            }
            set
            {
                selectedFunctionModule = value;

            }
        }


        /// <summary>
        /// the flag of displaying the check box
        /// </summary>
        public bool DisplayCheckBox
        {
            get
            {
                if (ViewState["DisplayCheckBox_forpermernant"] == null)
                {
                    return false;
                }
                return (bool)ViewState["DisplayCheckBox_forpermernant"];
            }
            set
            {
                ViewState["DisplayCheckBox_forpermernant"] = value;
            }
        }

        /// <summary>
        /// place the function nodes to this list
        /// </summary>
        private List<LeftMenuTreeView> treeNodeList;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.LoadLeftMenuTree();
                if (this.DisplayCheckBox)
                { this.tvLeftMenu.ShowCheckBoxes = TreeNodeTypes.All; }
                this.setTheSelectedNodes(this.tvLeftMenu.Nodes[0]);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tn"></param>
        private void goThroughTheTree(TreeNode tn)
        {

            if (tn.Checked)
            {
                if (this.selectedFunctionModule == null)
                {
                    this.selectedFunctionModule = new List<Guid?>();
                }
                this.selectedFunctionModule.Add(new Guid(tn.Value));
                this.setParent(tn.Parent);
            }
            if (tn.ChildNodes.Count != 0)
            {
                foreach (TreeNode tn1 in tn.ChildNodes)
                {
                    this.goThroughTheTree(tn1);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tn"></param>
        private void setTheSelectedNodes(TreeNode tn)
        {
            if (selectedFunctionModule == null)
            {
                return;
            }

            if (StringHelper.IsGUID(tn.Value))
            {
                if (this.selectedFunctionModule.Contains(new Guid(tn.Value)))
                {
                    tn.Checked = true;
                }
            }

            if (tn.ChildNodes.Count != 0)
            {
                foreach (TreeNode tn1 in tn.ChildNodes)
                {
                    this.setTheSelectedNodes(tn1);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tn"></param>
        private void setParent(TreeNode tn)
        {
            if (string.IsNullOrEmpty(tn.Value))
            {
                return;
            }
            if (StringHelper.IsGUID(tn.Value))
            {
                if (!this.selectedFunctionModule.Contains(new Guid(tn.Value)))
                {
                    this.selectedFunctionModule.Add(new Guid(tn.Value));
                    if (tn.Parent != null)
                    {
                        this.setParent(tn);
                    }
                }
            }
        }

        /// <summary>
        /// bind the left menu tree via the administrator's permission
        /// </summary>
        private void LoadLeftMenuTree()
        {
            //pseudocode
            Sys_User sUser = new Sys_User();
            sUser.ID = base.MyBasePage.CurrentUserID;
            if (base.MyBasePage.CurrentRole != null)
            {
                foreach (Sys_Role sr in base.MyBasePage.CurrentRole)
                {
                    if (sr.IsSuperGroup)
                    {
                        sUser.IsSuperAdministrator = sr.IsSuperGroup;
                    }
                }
            }

            //pls add the real sys_user info the instance above

            treeNodeList = this.sys_LeftMenuManagement.LoadLeftMenuTree(sUser);
            TreeNode tn = new TreeNode();
            tn.Text = "管理后台";

            this.tvLeftMenu.Nodes.Add(tn);
            if (treeNodeList == null)
            {
                return;
            }
            foreach (LeftMenuTreeView s in treeNodeList.Where(tnl => tnl.ParentID == null))
            {
                this.AppendChildNode(s, tn);
            }
        }

        /// <summary>
        /// recursion bind the tree view
        /// </summary>
        /// <param name="sltree"></param>
        /// <param name="matherNode"></param>
        private void AppendChildNode(LeftMenuTreeView sltree, TreeNode matherNode)
        {
            TreeNode tn = new TreeNode();
            tn.Text = sltree.FunctionName;
            tn.Value = sltree.ID.ToString();
            tn.NavigateUrl = sltree.NavigationUrl;
            matherNode.ChildNodes.Add(tn);
            List<LeftMenuTreeView> list = this.treeNodeList.Where(t => t.ParentID == sltree.ID && t.ParentID != null).ToList();

            foreach (LeftMenuTreeView sl in list)
            {
                AppendChildNode(sl, tn);
            }
        }
    }
}